// 简单git提交脚本编写 原理:命令行参数和程序替换
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using std::cout;
using std::endl;

int main(int argc, char* argv[])
{
    int status;
    if (argc != 2)
    {
        cout << "请输入需要git提交的日志:" << endl;
        exit(1);
    }

    int id1 = fork();
    if (id1 == 0)
    {
        execlp("git", "git", "add", "--all", NULL);
        exit(1);
    }
    wait(&status);
    if (WEXITSTATUS(status) == 1)
    {
        cout << "执行git add 失败" << endl;
        exit(1);
    }

    int id2 = fork();
    if (id2 == 0)
    {
        execlp("git", "git", "commit", "-m", argv[1], NULL);
        exit(2);
    }
    wait(&status);
    if (WEXITSTATUS(status) == 2)
    {
        cout << "执行git commit 失败" << endl;
        exit(1);
    }

    int id3 = fork();
    if (id3 == 0)
    {
        execlp("git", "git", "push", NULL);
        exit(3);
    }
    wait(&status);
    if (WEXITSTATUS(status) == 3)
    {
        cout << "执行git push 失败" << endl;
        exit(1);
    }
    return 0;
}